﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;

namespace Viewer.BusinessObject.BusinessEntities
{
    public partial class DiaChi : BusinessBase
    {
        DataClassesDataContext db = new DataClassesDataContext();
        CommonCode common = new CommonCode();
        /// <summary>
        /// Loads all data for diachi.
        /// </summary>
        /// <returns></returns>
        /// <author>baodt</author>
        /// <datetime>7/4/2011-3:56 PM</datetime>
        public IEnumerable LoadAllDataForDiachi()
        {
          
            var query = from a in db.CHDiaChis
                        select new
                        {
                            DiaChiID = a.DiaChiID,
                            SoNha = a.SoNha,
                            ThonPho = a.ThonPho,
                            XaPhuong = a.XaPhuongID,
                            QuanHuyenID = a.QuanHuyenID,
                            TinhTPID = a.TinhTPID,
                            GhiChu = a.GhiChu,
                            TinhName = common.LoadTenHanhChinhById(a.TinhTPID),
                            QuanName = common.LoadTenHanhChinhById(a.QuanHuyenID),                            
                            XaPhuongName = common.LoadTenHanhChinhById(a.XaPhuongID)
                        };

            return query.ToList();
        }
        public CHDiaChi LoadDataByDiaChiId(int id)
        {
            return this.Context.CHDiaChis.Where(a => a.DiaChiID.Equals(id)).SingleOrDefault();

        }
        /// <summary>
        /// Loads the data by condition.
        /// </summary>
        /// <param name="sonha">The sonha.</param>
        /// <param name="ghichu">The ghichu.</param>
        /// <returns></returns>
        /// <author>baodt</author>
        /// <datetime>7/4/2011-3:56 PM</datetime>
        public IEnumerable LoadDataByCondition(string sonha, int xa, string thon, int quan, int thanhpho)
        {
            var query = (from a in db.CHDiaChis
                        select new
                        {
                            DiaChiID = a.DiaChiID,
                            SoNha = a.SoNha,
                            ThonPho = a.ThonPho,
                            XaPhuong = a.XaPhuongID,
                            QuanHuyenID = a.QuanHuyenID,
                            TinhTPID = a.TinhTPID,
                            GhiChu = a.GhiChu,
                            TinhName = common.LoadTenHanhChinhById(a.TinhTPID),
                            QuanName = common.LoadTenHanhChinhById(a.QuanHuyenID),
                            XaPhuongName = common.LoadTenHanhChinhById(a.XaPhuongID)
                        }).ToList();
            if (sonha != string.Empty)
            {
                query = query.Where(a => a.SoNha.ToLower().Contains(sonha.ToLower())).ToList();
            }

            if (xa > 0)
            {
                query = query.Where(a => a.XaPhuong.Equals(xa)).ToList();
            }
            if (!string.IsNullOrEmpty(thon))
            {
                query = query.Where(a => a.ThonPho.Contains(thon)).ToList();
            }
            if (quan > 0)
            {
                query = query.Where(a => a.QuanHuyenID.Equals(quan)).ToList();
            }
            if (thanhpho > 0)
            {
                query = query.Where(a => a.TinhTPID.Equals(thanhpho)).ToList();
            }
            return query;
        }
        /// <summary>
        /// Inserts the dia chi.
        /// </summary>
        /// <param name="soNha">The so nha.</param>
        /// <param name="phuong">The phuong.</param>
        /// <param name="thonPho">The thon pho.</param>
        /// <param name="quanID">The quan ID.</param>
        /// <param name="thanhphoId">The thanhpho id.</param>
        /// <param name="ghiChu">The ghi chu.</param>
        /// <author>baodt</author>
        /// <datetime>7/4/2011-3:56 PM</datetime>
        public void InsertDiaChi(string soNha, int phuong, string thonPho, int quanID, int thanhphoId, string ghiChu)
        {
            using (var context = new DataClassesDataContext())
            {
                CHDiaChi item = new CHDiaChi();
                item.SoNha = soNha;
                item.ThonPho = thonPho;
                item.XaPhuongID = phuong;
                item.QuanHuyenID = quanID;
                item.TinhTPID = thanhphoId;
                item.GhiChu = ghiChu;
                context.CHDiaChis.InsertOnSubmit(item);
                context.SubmitChanges();
            }

        }
        public void UpdateDiachi(int id, string soNha, int phuong, string thonPho, int quanID, int thanhphoId, string ghiChu)
        {
            using (var context = new DataClassesDataContext())
            {
                CHDiaChi item = context.CHDiaChis.Where(a => a.DiaChiID.Equals(id)).SingleOrDefault();
                item.SoNha = soNha;
                item.ThonPho = thonPho;
                item.XaPhuongID = phuong;
                item.QuanHuyenID = quanID;
                item.TinhTPID = thanhphoId;
                item.GhiChu = ghiChu;

                context.SubmitChanges();
            }

        }
        public void DeleteDiaChi(int id)
        {
            using (var context = new DataClassesDataContext())
            {
                var item = context.CHDiaChis.Single(a => a.DiaChiID.Equals(id));
                context.CHDiaChis.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
        }


    }
}
